package com.zeros.mysplashtwo.view.weidge;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.webkit.GeolocationPermissions;
import android.webkit.WebView;
import android.widget.ProgressBar;

import com.zeros.mysplashtwo.utils.ZlogUtilTwo;

/**
 * 自定义带进度条的WebView on 2018/1/13.
 */

public class ProgressWebView extends WebView {

    private ProgressBar progressBar;


    public ProgressWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        progressBar = new ProgressBar(context, null, android.R.attr.progressBarStyleHorizontal);
        progressBar.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 12, 0, 0));
        // progressbar.setBackgroundColor(getResources().getColor(R.color.colorBlackFont));
        //progressbar.setDrawingCacheBackgroundColor(getResources().getColor(R.color.colorBlackFont));

        addView(progressBar);
//        setWebViewClient(new WebViewClient(){});
        setWebChromeClient(new WebChromeClient());
    }

    public class WebChromeClient extends android.webkit.WebChromeClient {
        @Override
        public void onReceivedIcon(WebView view, Bitmap icon) {
            super.onReceivedIcon(view, icon);
        }

        @Override
        public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
            callback.invoke(origin, true, true);
            super.onGeolocationPermissionsShowPrompt(origin, callback);
        }

        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if (newProgress == 100) {
                progressBar.setVisibility(GONE);
            } else {
                if (progressBar.getVisibility() == GONE)
                    progressBar.setVisibility(VISIBLE);
                progressBar.setProgress(newProgress);
                ZlogUtilTwo.e("====>" + String.valueOf(newProgress));
            }
            super.onProgressChanged(view, newProgress);
        }
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        LayoutParams params = (LayoutParams) progressBar.getLayoutParams();
        params.x = l;
        params.y = t;
        progressBar.setLayoutParams(params);
        super.onScrollChanged(l, t, oldl, oldt);
    }


}
